package fileIO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedStreamTest {
	public static void main(String[] args) throws IOException {
		BufferedOutputStreamTest();
		BufferedInputStreamTest();
	}

	private static void BufferedInputStreamTest() throws IOException {
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(
				"/home/tarena/Desktop/4.txt"));
		int b;
		//每次读取都是从内存中读取
		while ((b = in.read()) != -1) {
			System.out.println(b);
		}
		in.close();
	}

	private static void BufferedOutputStreamTest() throws IOException {
		FileOutputStream fout = new FileOutputStream(
				"/home/tarena/Desktop/4.txt");
		BufferedOutputStream out = new BufferedOutputStream(fout, 1024);// 默认缓冲区8192
		// 若写出的字节数小于缓冲区的容量，不输出的文件
		out.write(97);
		out.write(98);
		out.write(99);
		// 调用flush方法将缓冲区数据输出
		out.flush();
		// close方法会先调用flush方法，最好自己手动调用一下
		out.close();// 会关闭fout
	}

}
